package com.situ.fly.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.situ.fly.interceptor.UserInterceptor;

/**
 * 配置SpringMVC的处理过程 
 * 
 * @author bobzyh
 *
 */
@Configuration // 这是一个配置类, 配置SpringMVC
public class WebConfig implements WebMvcConfigurer{

	/**
	 * 创建拦截器的对象
	 */
	@Autowired
	private UserInterceptor userInterceptor;
	
	/**
	 * 在SpringMVC上注册这个拦截器, 并配置需要拦截的页面
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		// 注册用户登录的拦截器
		registry.addInterceptor(userInterceptor)
			.addPathPatterns(new String[] {
					"/user/set",
					"/user/head",
					"/user/resetPassword",
					"/question/add"
			})	//配置需要拦截的路径 
			;
	}
	
	/**
	 * 静态资源映射
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 注册upload路径与D:\img\文件夹的映射关系
		registry.addResourceHandler("/upload/**")	// 当访问/upload/**任何文件
			.addResourceLocations("file:D:\\img\\"); // 到D:\img\文件夹中去找

	}
	
	
}
